package online.exam.controller;

import online.exam.common.GlobalConstant;
import online.exam.pojo.Teacher;
import online.exam.pojo.bo.TeacherBO;
import online.exam.pojo.vo.TeacherVO;
import online.exam.service.TeacherService;
import online.exam.utils.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author 王景煜
 * created in 2019/11/16 14:51
 */

@RestController
@RequestMapping(value = "/teacher")
public class TeacherController {

    @Autowired
    private TeacherService teacherServiceImpl;

    @GetMapping("/queryByNoAndPwd")
    public Teacher queryByNoAndPwd(TeacherBO teacherBO, HttpServletRequest request) {
        HttpSession session = request.getSession();
        Teacher teacher = teacherServiceImpl.queryByNoAndPwd(teacherBO);
        if (teacher != null) {
            session.setAttribute(GlobalConstant.TEACHER, teacher);
            return teacher;
        }
        return null;
    }

    @GetMapping("/queryTeaByNoAndPwd")
    public Teacher queryTeaByNoAndPwd(TeacherBO teacherBO) {
        return teacherServiceImpl.queryByNoAndPwd(teacherBO);
    }

    @GetMapping("/isLogin")
    public Teacher isLogin(HttpServletRequest request) {
        return (Teacher) request.getSession().getAttribute(GlobalConstant.TEACHER);
    }

    @GetMapping("/queryAll/{page}/{limit}")
    public PageUtils<TeacherVO> queryAll(@PathVariable Integer page, @PathVariable Integer limit) {
        return teacherServiceImpl.queryAll(page, limit);
    }

    @GetMapping("/queryById/{id}")
    public Teacher queryById(@PathVariable String id) {
        return teacherServiceImpl.queryById(id);
    }

    @GetMapping("/queryByNo")
    public Teacher queryByNo(@RequestParam String teacherNo) {
        return teacherServiceImpl.queryByNo(teacherNo);
    }

    @GetMapping("/queryByCondition")
    public List<Teacher> queryByCondition(Teacher teacher) {
        return teacherServiceImpl.queryByCondition(teacher);
    }

    @PostMapping("/addTeacher")
    public int addTeacher(Teacher teacher) {
        return teacherServiceImpl.addTeacher(teacher);
    }

    @PostMapping("/updateTeacher")
    public int updateTeacher(Teacher teacher) {
        return teacherServiceImpl.updateTeacher(teacher);
    }

    @PostMapping("/deleteTeacher")
    public int deleteTeacher(@RequestParam String id) {
        return teacherServiceImpl.deleteTeacher(id);
    }

}
